package com.mmall.service;

import com.google.common.base.Preconditions;
import com.mmall.beans.PageQuery;
import com.mmall.beans.PageResult;
import com.mmall.common.RequestHolder;
import com.mmall.dao.SysUserMapper;
import com.mmall.exception.ParamException;
import com.mmall.model.SysUser;
import com.mmall.param.UserParam;
import com.mmall.util.BeanValidator;
import com.mmall.util.IpUtil;
import com.mmall.util.MD5Util;
import com.mmall.util.PasswordUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class SysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private SysLogService sysLogService;

    public void save(UserParam param){
        BeanValidator.check(param);
        if (checkTelephoneExist(param.getTelephone(), param.getId())){
            throw new ParamException("电话已被占用");
        }
        if (checkEmailExist(param.getMail(), param.getId())){
            throw new ParamException("邮箱已被占用");
        }
        String password = PasswordUtil.randomPassword();
        //TODO
        password = "12345678";
        String encryptedPassword = MD5Util.encrypt(password);
        SysUser user = SysUser.builder().username(param.getUsername()).telephone(param.getTelephone()).mail(param.getMail())
                            .password(encryptedPassword).deptId(param.getDeptId()).status(param.getStatus()).remark(param.getRemark()).build();
        user.setOperateIp("127.0.0.1");
        user.setOperator(RequestHolder.getCurrentUser().getUsername());
        user.setOperateTime(new Date());

        //TODO: sendEmail

        sysUserMapper.insertSelective(user);
        sysLogService.saveUserLog(null, user);
    }

    public void update(UserParam param){
        BeanValidator.check(param);
//        if (checkTelephoneExist(param.getTelephone(), param.getId())){
//            throw new ParamException("电话已被占用");
//        }
//        if (checkEmailExist(param.getMail(), param.getId())){
//            throw new ParamException("邮箱已被占用");
//        }

        SysUser before = sysUserMapper.selectByPrimaryKey(param.getId());
        Preconditions.checkNotNull(before, "待更新的用户不存在");
        SysUser after = SysUser.builder().id(param.getId()).username(param.getUsername()).telephone(param.getTelephone())
                            .mail(param.getMail()).deptId(param.getDeptId()).status(param.getStatus())
                            .remark(param.getRemark()).build();
        sysUserMapper.updateByPrimaryKeySelective(after);

        after.setOperateIp(IpUtil.getRemoteIp(RequestHolder.getCurrentRequest()));
        after.setOperator(RequestHolder.getCurrentUser().getUsername());
        after.setOperateTime(new Date());
    }

    public boolean checkEmailExist(String email, Integer userId){
        return sysUserMapper.countByMail(email, userId) > 0;
    }

    public boolean checkTelephoneExist(String telephone, Integer userId){
        return sysUserMapper.countByTelephone(telephone, userId) > 0;
    }

    public SysUser findByKeyword(String keyword){
        return sysUserMapper.findByKeyword(keyword);
    }

    public PageResult<SysUser> getPageByDeptId(int deptId, PageQuery page){
        BeanValidator.check(page);
        int count = sysUserMapper.countByDeptId(deptId);
        if (count > 0){
            List<SysUser> list = sysUserMapper.getPageByDeptId(deptId, page);
            PageResult<SysUser> result = new PageResult();
            result.setData(list);
            result.setTotal(count);
            return result;
        }
            return new PageResult<SysUser>();
    }

    public List<SysUser> getAll(){
        return sysUserMapper.getAll();
    }
}
